namespace SimpleWebServices.Security.Cryptography
{
    using System.Xml.Serialization;

    [XmlRoot("password")]
    public sealed class SecureHashPassword : ValueObject<SecureHashPassword>
    {
        public SecureHashPassword()
        {
            this.RegisterProperty(x => x.Hash);
            this.RegisterProperty(x => x.Salt);
        }

        [XmlAttribute("hash")]
        public string Hash
        {
            get;
            set;
        }

        [XmlAttribute("salt")]
        public string Salt
        {
            get;
            set;
        }

        public static SecureHashPassword Compute(string password)
        {
            return SecureHashPassword.Compute(password, new Salt());
        }

        public static SecureHashPassword Compute(string password, Salt salt)
        {
            return new SecureHashPassword
            {
                Salt = salt,
                Hash = SecureHash.Encrypt(password, salt)
            };
        }

        public bool Authenticate(string password)
        {
            return this.Equals(SecureHashPassword.Compute(password, this.Salt));
        }
    }
}